﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.UserSkins;
using COMOSOFT.Class;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using COMOSOFT.MatarialManagement.Forms;
using DevExpress.XtraGrid.Menu;
using DevExpress.Utils.Menu;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using System.IO;
using System.Data.SqlClient;

namespace COMOSOFT.OrderBusiness.Forms
{
    public partial class TabOrderDetails : Form
    {
        ClassShare cls = new ClassShare("TabOrderDetails");
        FrmPreviewOrder frPvOrder = null;
        //Task task;
        DataTable dtbDelivery,dtbPayment;
        int MPOID;
        string Cusname, Group, AddressCus, Phone, AccountName, BankAccount, AddressBank, SwiftCode, Field, Vendor, Remarks;
        bool add = false;
        string sql;
        int OID;
        double price;
        GridView cview = null;
        FrmMALibrary frmMALib = null;
        List<string> ListItem = new List<string>();

        public TabOrderDetails(int _MPOID)
        {
            InitializeComponent();
            this.MPOID = _MPOID;  
            dtbDelivery=cls.GetData("sp_OrderGetDeliveryTerm");
            dtbPayment = cls.GetData("sp_OrderGetPaymentTerm");
            cls.AutoComplete(txt_DeliveryTerm, dtbDelivery);
            cls.AutoComplete(txt_PaymentTerm, dtbPayment);
        }
        private void TabOrderDetails_Load(object sender, EventArgs e)
        {            
                
        }

        #region [Load Data]
        //public void LoadData()
        //{
        //    try
        //    {
        //        if (task == null || task.Status != TaskStatus.Running)
        //        {
        //            cls.LoadLayout(gridView_OrderDetails, ClassShare.lcOrder + "LayOutGaneralView.XML");
        //            splashScreenManager_OrderDetails.ShowWaitForm();
        //            gridControl_OrderDetails.DataSource = null;
        //            task = Task.Factory.StartNew(() => CallData());
        //            task.ContinueWith(tt => FillTable());
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message);
        //    }          
        //}

        //private delegate void dlgFillTable();
        //private void FillTable()
        //{
        //    try
        //    {
        //        if (InvokeRequired)
        //        {
        //            this.Invoke(new dlgFillTable(FillTable));
        //        }
        //        else
        //        {
        //            gridControl_OrderDetails.DataSource = dataSetOrderManagement.OrderDetails;
        //            splashScreenManager_OrderDetails.CloseWaitForm();
        //        }
        //    }
        //    catch
        //    {
        //        MessageBox.Show("Progess fail");
        //    }
        //}
        public void LoadData()
        {
            try
            {
                sp_CustomerListTableAdapter.Fill(dataSetOrderManagement.sp_CustomerList, ClassShare.Usid);
                if (MPOID > 0)
                {
                    masterPOTableAdapter.Fill(this.dataSetOrderManagement.MasterPO, MPOID);
                    orderDetailsTableAdapter.Fill(this.dataSetOrderManagement.OrderDetails, MPOID);
                    orderSizeBreakdownTableAdapter.Fill(dataSetOrderManagement.OrderSizeBreakdown);                    
                    employeeTableAdapter.Fill(this.dataSetOrderManagement.Employee);
                    itemTableAdapter.Fill(this.dataSetOrderManagement.Item);
                    sp_OrderGetMATableAdapter.Fill(this.dataSetMatarial.sp_OrderGetMA);    
                    this.Text = txt_Customer.EditValue.ToString() + "_" + txt_ContSalePO.EditValue.ToString();
                    MPOID = Convert.ToInt32(txt_MPOID.Text);
                    groupControl_OrderDetails.Enabled = true;
                    gridView_OrderDetails.ColumnPanelRowHeight = 30;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        #region[AddNew]
        public void AddNew()
        {
            employeeTableAdapter.FillBy(this.dataSetOrderManagement.Employee,ClassShare.Usid);
            masterPOBindingSource.AddNew();
            txt_Creater.EditValue = Convert.ToInt64(ClassShare.Usid);
            txt_IssuaDate.EditValue = DateTime.Now.ToShortDateString();            
        }
        #endregion

        #region[Update Data]
        public void UpdateData(string txt)
        {
            try
            {
                if (Convert.ToInt32(txt_Creater.EditValue) == ClassShare.Usid)
                {
                    if (txt == "Update")
                    {
                        masterPOBindingSource.EndEdit();
                        masterPOTableAdapter.Update(dataSetOrderManagement.MasterPO);
                        dataSetOrderManagement.MasterPO.AcceptChanges();
                        MPOID = Convert.ToInt32(txt_MPOID.EditValue);
                    }
                    else
                    {                     
                        if (MessageBox.Show("Are you sure delete?", "Confifm delete...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            dataSetOrderManagement.MasterPO.Rows[0].Delete();
                            masterPOTableAdapter.Update(dataSetOrderManagement.MasterPO);
                            dataSetOrderManagement.MasterPO.AcceptChanges();
                            this.Close();
                        }                        
                    }
                }
                else
                    MessageBox.Show("You no permission!");
           }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region[Customer List]
        private void HideButton(bool val1, bool val2)
        {
            dataNavigator_Customer.Buttons.CustomButtons[2].Enabled = val1;
            dataNavigator_Customer.Buttons.CustomButtons[0].Enabled = val2;
            dataNavigator_Customer.Buttons.CustomButtons[3].Enabled = val2;
            groupControl_CustomerList.Enabled = val2;
        }

        private void dataNavigator_Customer_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            switch (e.Button.Tag.ToString())
            {
                case "Add":
                    HideButton(true, false);
                    txt_CustomerName.Properties.ReadOnly = false;
                    txt_CustomerName.Focus();
                    txt_CustomerName.Text = "";
                    txt_Group.Text = "";
                    txt_Address.Text = "";
                    txt_Phone.Text = "";
                    txt_AccountName.Text = "";
                    txt_BankAccount.Text = "";
                    txt_BankAddress.Text = "";
                    txt_SwiftCode.Text = "";
                    txt_Feild.Text = "";
                    txt_Vendor.Text = "";
                    txt_Remarks.Text = "";
                    add = true;
                    break;
                case "Save":
                    try
                    {
                        Cusname = txt_CustomerName.Text;
                        Group = txt_Group.Text;
                        AddressCus = txt_Address.Text;
                        Phone = txt_Phone.Text;
                        AccountName = txt_AccountName.Text;
                        BankAccount = txt_BankAccount.Text;
                        AddressBank = txt_BankAddress.Text;
                        SwiftCode = txt_SwiftCode.Text;
                        Field = txt_Feild.Text;
                        Vendor = txt_Vendor.Text;
                        Remarks = txt_Remarks.Text;
                        if (add == false)
                        {
                            sql = "sp_CustomerUpdate";
                        }
                        else
                        {
                            sql = "sp_CustomerInsert";
                        }
                        cls.UpdateData(sql, new string[] { "@Customer", "@Group", "@AddressCus", "@Phone", "@AccountName", "@BankAccount", "@AddressBank", "@SwiftCode", "@Field", "@Vendor", "@Remarks", "@EID" },
                                          new object[] { Cusname, Group, AddressCus, Phone, AccountName, BankAccount, AddressBank, SwiftCode, Field, Vendor, Remarks, ClassShare.Usid });
                        sp_CustomerListTableAdapter.Fill(dataSetOrderManagement.sp_CustomerList, ClassShare.Usid);
                        HideButton(false, true);
                        txt_CustomerName.Properties.ReadOnly = true;
                        add = false;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

                    break;
                case "Cancel":
                    HideButton(false, true);
                    gridView_CustomerList.FocusedRowHandle = 0;
                    txt_CustomerName.Properties.ReadOnly = true;
                    break;
                case "Refresh":
                    sp_CustomerListTableAdapter.Fill(dataSetOrderManagement.sp_CustomerList, ClassShare.Usid);
                    break;
                case "Exit":
                    dockPanel_Customer.Hide();
                    break;
            }
        }
       

        private void gridView_CustomerList_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                GridView view = sender as GridView;
                txt_CustomerName.Text = view.GetFocusedDataRow()["Customer"].ToString();
                txt_Group.Text = view.GetFocusedDataRow()["Group"].ToString();
                txt_Address.Text = view.GetFocusedDataRow()["AddressCus"].ToString();
                txt_Phone.Text = view.GetFocusedDataRow()["Phone"].ToString();
                txt_AccountName.Text = view.GetFocusedDataRow()["AccountName"].ToString();
                txt_BankAccount.Text = view.GetFocusedDataRow()["BankAccount"].ToString();
                txt_BankAddress.Text = view.GetFocusedDataRow()["AddressBank"].ToString();
                txt_SwiftCode.Text = view.GetFocusedDataRow()["SwiftCode"].ToString();
                txt_Feild.Text = view.GetFocusedDataRow()["Field"].ToString();
                txt_Vendor.Text = view.GetFocusedDataRow()["Vendor"].ToString();
                txt_Remarks.Text = view.GetFocusedDataRow()["Remarks"].ToString();
            }
            catch
            {
            }
        }
        #endregion

        private void txt_Customer_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Tag.ToString() == "Add")
            {
                dockPanel_Customer.Show();                
            }
        }

        private void gridView_OrderDetails_InitNewRow(object sender, InitNewRowEventArgs e)
        {
            try
            {
                GridView view = sender as GridView;
                view.SetFocusedRowCellValue("MPOID", MPOID);
                view.SetRowCellValue(e.RowHandle, "EID", ClassShare.Usid);
                view.SetRowCellValue(e.RowHandle, "PORCVD", DateTime.Now);
                view.SetRowCellValue(e.RowHandle, "CusExDate", DateTime.Now);
                view.SetRowCellValue(e.RowHandle, "Type", "Product");
                view.SetRowCellValue(e.RowHandle, "Status", "On going");
                view.SetRowCellValue(e.RowHandle, "PO", txt_ContSalePO.EditValue.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region[Update Order Details]
        private void gridView_OrderDetails_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            try
            {
                GridView view = sender as GridView;
                if (view.IsNewItemRow(e.RowHandle))
                {
                    orderDetailsBindingSource.EndEdit();
                    orderDetailsTableAdapter.Update(dataSetOrderManagement.OrderDetails);
                    dataSetOrderManagement.OrderDetails.AcceptChanges();                    
                }
                else
                {
                    if (ClassShare.Usid == Convert.ToInt32(view.GetFocusedDataRow()["EID"]))
                    {
                        orderDetailsBindingSource.EndEdit();
                        orderDetailsTableAdapter.Update(dataSetOrderManagement.OrderDetails);
                        dataSetOrderManagement.OrderDetails.AcceptChanges();                        
                    }
                    else
                    {
                        MessageBox.Show("You don't permission!", "Can't update....", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region[ContextMenu]
        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                GridView view = gridView_OrderDetails as GridView;
                if (ClassShare.Usid == Convert.ToInt32(view.GetFocusedDataRow()["EID"]))
                {
                    if (XtraMessageBox.Show("Are you sure?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        cls.UpdateData("sp_OrderDetailsDelete", new string[] { "@OID" }, new object[] { Convert.ToInt32(gridView_OrderDetails.GetFocusedDataRow()["OID"]) });
                        orderDetailsTableAdapter.Fill(dataSetOrderManagement.OrderDetails, MPOID);
                    }
                }
                else
                {
                    MessageBox.Show("You don't permission!", "Can't delete....", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }     
            }
            catch
            {
                MessageBox.Show("Can't delete object");
            }
        }

        private void sizeBreakdownToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (gridView_OrderDetails.FocusedRowHandle >= 0)
            {
                int OID = Convert.ToInt32(gridView_OrderDetails.GetFocusedDataRow()["OID"]);
                FrmSizeBreakdown frSBD = new FrmSizeBreakdown();
                frSBD.OIDs.Add(OID);
                frSBD.Show();
                LoadData();
            }
        }

        private void factoryPurchaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridView_OrderDetails.FocusedRowHandle >= 0)
                {
                    FrmFactoryPurchaseMaster frFPM = new FrmFactoryPurchaseMaster(-1) { MdiParent = ClassShare.frmain, Text = "PO: Add new Factory Purchase" };
                    int[] numrow = gridView_OrderDetails.GetSelectedRows();
                    foreach (int i in numrow)
                    {
                        DataRow row = gridView_OrderDetails.GetDataRow(i);
                        if (i == 0)
                            frFPM.OIDS = row["OID"].ToString();
                        else
                            frFPM.OIDS += "," + row["OID"].ToString();
                    }
                    frFPM.Show();
                    frFPM.LoadFPM();
                    frFPM.NewFPM();
                    ClassShare.frmain.SelectedPageGroup("Purchasing View");
                    ClassShare.frmain.Savelayout(this.Text, "Order Management", "Order General");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void exportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PreviewOrder();
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            orderDetailsTableAdapter.Fill(dataSetOrderManagement.OrderDetails, MPOID);
            orderSizeBreakdownTableAdapter.Fill(dataSetOrderManagement.OrderSizeBreakdown);
        }

        private void saveLayoutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            cls.SaveLayout(gridView_OrderDetails, ClassShare.lcOrder + "LayOutGaneralView.XML");
        }

        private void deleteSizeBreakdownToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {                
                if (XtraMessageBox.Show("Are you sure delete?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    cls.UpdateData("sp_SizeBreakdownDelete", new string[] { "@SBID" }, new object[] { Convert.ToInt32(cview.GetFocusedDataRow()["SBID"]) });
                    orderSizeBreakdownTableAdapter.Fill(dataSetOrderManagement.OrderSizeBreakdown);
                }               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region[Update SizeBreakdown]
        private void gridView_SizeBreakDown_InitNewRow(object sender, InitNewRowEventArgs e)
        {           
            gridView_SizeBreakDown.SetFocusedRowCellValue("OID", Convert.ToInt32(gridView_OrderDetails.GetFocusedDataRow()["OID"]));
        }

        private void gridView_SizeBreakDown_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            try
            {
                orderSizeBreakdownBindingSource.EndEdit();
                orderSizeBreakdownTableAdapter.Update(dataSetOrderManagement.OrderSizeBreakdown);
                dataSetOrderManagement.OrderSizeBreakdown.AcceptChanges();
                orderSizeBreakdownTableAdapter.Fill(dataSetOrderManagement.OrderSizeBreakdown);

                OID = Convert.ToInt32(gridView_OrderDetails.GetFocusedDataRow()["OID"]);
                DataTable dtb = cls.GetData("sp_OrderPrepareQty", new string[] { "@OID" }, new object[] { OID });
                if (Convert.ToInt32(dtb.Rows[0]["Qty"]) < 0)
                    cls.UpdateData("sp_OrderUpdateQty", new string[] {"@OID","@Qty" }, new object[] {OID,Convert.ToInt32(dtb.Rows[0]["SBQty"])});
               // LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }        
        #endregion       

        private void gridView_OrderDetails_MasterRowExpanded(object sender, CustomMasterRowEventArgs e)
        {
            GridView view = gridView_OrderDetails.GetDetailView(gridView_OrderDetails.FocusedRowHandle, 0) as GridView;
            cview = view;
        }

        #region [Choose Art]

        private void newFabricToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmMALib = new FrmMALibrary(-1);
            frmMALib.LoadData();
            frmMALib.ShowButton();
            frmMALib.Show();
            sp_OrderGetMATableAdapter.Fill(dataSetMatarial.sp_OrderGetMA);
        }

        private void newColorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                GridView view = gridView_ChooseMA as GridView;
                if (!view.IsGroupRow(view.FocusedRowHandle))
                {
                    int MAID = Convert.ToInt32(view.GetFocusedDataRow()["MAID"]);
                    frmMALib = new FrmMALibrary(MAID);
                    frmMALib.LoadData();
                    frmMALib.ShowButton();
                    frmMALib.Show();
                    sp_OrderGetMATableAdapter.Fill(dataSetMatarial.sp_OrderGetMA);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion   

        private void repositoryItemPopupContainerEditChooseArt_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            dockPanel_ChooseArt.Show();
        }

        #region[Style Update]
        private void gridView_Style_InitNewRow(object sender, InitNewRowEventArgs e)
        {
            gridView_Style.SetFocusedRowCellValue("Customer", txt_Customer.EditValue.ToString());
            gridView_Style.SetFocusedRowCellValue("Creater", Convert.ToInt64(ClassShare.Usid));
        }

        private void gridView_Style_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            try
            {
                 GridView view =sender as GridView;
                int USID = Convert.ToInt32(view.GetFocusedDataRow()["Creater"]);
                if (ClassShare.Usid == USID)
                {
                    orderStyleListsBindingSource.EndEdit();
                    orderStyleListsTableAdapter.Update(dataSetOrderManagement.OrderStyleLists);
                    dataSetOrderManagement.OrderStyleLists.AcceptChanges();
                    orderStyleListsTableAdapter.Fill(dataSetOrderManagement.OrderStyleLists, txt_Customer.EditValue.ToString());                    
                }
                else
                {
                    MessageBox.Show("You don't permission!", "Can't update....", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void repositoryItemPopupContainerEdit_Style_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            dockPanel_Style.Show();
            orderStyleListsTableAdapter.Fill(dataSetOrderManagement.OrderStyleLists, txt_Customer.EditValue.ToString());            
        }
        #endregion

        #region[choose Style]
        private void dataNavigator_Style_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            if (e.Button.Tag.ToString() == "Add")
            {
                try
                {
                    GridView view = gridView_Style as GridView;
                    GridView Orderview = gridView_OrderDetails as GridView;
                    Orderview.SetFocusedRowCellValue("StyleNo", view.GetFocusedDataRow()["StyleNo"].ToString());
                    Orderview.SetFocusedRowCellValue("Item", view.GetFocusedDataRow()["ItemGroup"].ToString());
                    if ((Convert.IsDBNull(view.GetFocusedDataRow()["Price"]) ? "" : view.GetFocusedDataRow()["Price"].ToString()) != "")
                    {
                        price = Convert.ToDouble(view.GetFocusedDataRow()["Price"]);
                        Orderview.SetFocusedRowCellValue("SalePrice", price);
                    }                    
                    dockPanel_Style.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                orderStyleListsTableAdapter.Fill(dataSetOrderManagement.OrderStyleLists, txt_Customer.EditValue.ToString());
            }
        }
        #endregion

        #region[ChooseMA]
        private void dataNavigator_AddMA_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            if (e.Button.Tag.ToString() == "Add")
            {
                try
                {
                    GridView view = gridView_ChooseMA as GridView;
                    if (!view.IsGroupRow(view.FocusedRowHandle))
                    {
                        GridView Orderview = gridView_OrderDetails as GridView;
                        Orderview.SetFocusedRowCellValue("Art", view.GetFocusedDataRow()["Art"].ToString());
                        Orderview.SetFocusedRowCellValue("SupArt", view.GetFocusedDataRow()["SubArt"].ToString());
                        Orderview.SetFocusedRowCellValue("Color", view.GetFocusedDataRow()["Color"].ToString());
                        Orderview.SetFocusedRowCellValue("Content", view.GetFocusedDataRow()["Content"].ToString());
                    }

                    dockPanel_ChooseArt.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
                sp_OrderGetMATableAdapter.Fill(dataSetMatarial.sp_OrderGetMA);
        }
        #endregion

        #region[Payment Status]
        private void comboBox_PaymentStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox_PaymentStatus.SelectedIndex == 0)
            {
                panel_Percent.Visible = true;
                txt_Percent.Focus();
            }
            else
                panel_Percent.Visible = false;
        }
        #endregion

        #region[Copy/Paste]
        private void copyPasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                GridView view = gridView_OrderDetails as GridView;
                int[] rowHandles = view.GetSelectedRows();
                int[] destRowHandles = rowHandles;

                for (int j = 0; j < rowHandles.Length; j++)
                {
                    int rowHandle = rowHandles[j];
                    view.AddNewRow();
                    DataRow row = view.GetDataRow(view.FocusedRowHandle);
                    DataRow sourceRow = view.GetDataRow(rowHandle);
                    for (int i = 1; i < row.Table.Columns.Count; i++)
                    {
                        row[i] = sourceRow[i];
                    }
                    destRowHandles[j] = view.FocusedRowHandle;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region[DeleteStyle]
        private void deleteToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            GridView view =gridView_Style as GridView;
            if(view.FocusedRowHandle>=0)
            {
                if(ClassShare.Usid==Convert.ToInt32(view.GetFocusedDataRow()["Creater"]))
                {
                    if(MessageBox.Show("Are you sure delete item?","Confirm delete...",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
                    {
                        cls.UpdateData("sp_StyleDelete", new string[] { "@StyleNo", "@Customer" }, new object[] { view.GetFocusedDataRow()["StyleNo"].ToString(), view.GetFocusedDataRow()["Customer"].ToString() });
                        orderStyleListsTableAdapter.Fill(dataSetOrderManagement.OrderStyleLists, txt_Customer.EditValue.ToString());
                    }
                }
                else
                {
                    MessageBox.Show("You don't permission!");
                }
            }
        }
        private void refreshToolStripMenuItem4_Click(object sender, EventArgs e)
        {
            orderStyleListsTableAdapter.Fill(dataSetOrderManagement.OrderStyleLists, txt_Customer.EditValue.ToString());
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dockPanel_Style.Close();
        }
        #endregion

        #region[Item]
        private void repositoryItemLookUpEdit_Item_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Tag.ToString() == "Add")
            {
                dockPanel_Item.Show();
            }
        }       

        private void bnt_Cancel_Click(object sender, EventArgs e)
        {
            dockPanel_Item.Close();
        }

        private void bnt_OK_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = cls.connection();
                SqlCommand com = new SqlCommand("insert into Item (Item) values (@Item)", con);
                com.Parameters.AddWithValue("@Item", txt_Item.EditValue.ToString());
                com.ExecuteNonQuery();
                itemTableAdapter.Fill(dataSetOrderManagement.Item);
                dockPanel_Item.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region[Preview]
        public void PreviewOrder()
        {
            if (frPvOrder != null && !frPvOrder.IsDisposed)
            {
                frPvOrder.Select();
                frPvOrder.LoadData();
            }
            else
            {
                ListItem.Clear();
                ListItem.Add(txt_Customer.EditValue.ToString());
                ListItem.Add(txt_IssuaDate.EditValue.ToString());
                ListItem.Add(txt_ContSalePO.EditValue.ToString());
                ListItem.Add(txt_Unit.EditValue.ToString());
                ListItem.Add(txt_Qty.EditValue.ToString());
                ListItem.Add(xt_Description.EditValue.ToString());
                ListItem.Add(txt_DeliveryTerm.Text.ToString());
                ListItem.Add(txt_PaymentTerm.Text.ToString());
                ListItem.Add(txt_TotalAmount.EditValue.ToString());
                ListItem.Add(comboBox_PaymentStatus.EditValue.ToString());
                ListItem.Add(txt_Percent.EditValue.ToString());
                ListItem.Add(txt_Creater.Text.ToString());
                ListItem.Add(txt_Remark.EditValue.ToString());

                frPvOrder = new FrmPreviewOrder(gridView_OrderDetails, ListItem);
                frPvOrder.LoadData();
                frPvOrder.Show();
            }
        }
        #endregion

        private void gridView_ChooseMA_ColumnFilterChanged(object sender, EventArgs e)
        {
            gridView_ChooseMA.OptionsBehavior.AutoExpandAllGroups = true;
        }
    }
}
